package com.shuai.orderfood.exceptions;

import com.shuai.orderfood.common.HttpStatusCodeEnum;
import com.shuai.orderfood.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.Map;

/**
 * 全局异常处理器
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理系统定义的异常
     */
    @ExceptionHandler(OrderFoodException.class)
    public Result<Map<String, String>> handleValidationExceptions(OrderFoodException e) {
        log.info("exceptionCode=" + e.getCode());
        log.info("exceptionMsg=" + e.getMessage());
        return Result.fail(e.getCode(), e.getMessage());
    }

    /**
     * 处理其他异常
     */
    @ExceptionHandler(Exception.class)
    public Result<Void> handleException(Exception e) {
        e.printStackTrace();
        return Result.method(HttpStatusCodeEnum.SYSTEMEXCEPTION);
    }
}
